package com.yituo.mjworld.core.web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.utils.JwtTool;
import com.yituo.mjworld.core.utils.OtherTool;
import com.yituo.mjworld.core.web.core.MyHttpRequest;
import com.yituo.mjworld.mvc.entity.UserData;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.util.StringUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * 源码源自网络:http://blog.csdn.net/u013632854/article/details/73276992
 */
public class UploadServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
  /**
   * 上传目录名
   */
  private static final String UPLOAD_DIR = "uploadDir/img/";
  /**
   * 上传临时文件存储目录
   */
  private static final String TEMP_UPLOAD_DIR = "uploadDir/temp/";
  /**
   * 总上传文件最大为10M
   */
  private static final Long TOTAL_FILE_MAXSIZE = 10000000L;
  /**
   * 单个上传文件最大为10M
   */
  private static final int SINGLE_FILE_MAXSIZE = 2 * 1024 * 1024;

  private static final String[] extlist = {"jpg", "jpeg", "png"};

  @Override
  public void init() throws ServletException {
    super.init();
  }

  /**
   * doPost 请求
   *
   * @param request
   * @param response
   * @throws ServletException
   * @throws IOException
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("UploadTestServlet in");
    PrintWriter out = response.getWriter();
    String token = request.getHeader("token");
    String fieldName = request.getHeader("fieldName");
    if (StringUtils.isEmpty(fieldName)) {
      out.write("字段名不能为空!");
      return;
    }
    JSONObject result = JwtTool.validateToken(token);
    System.out.println(token);
    if (!result.getBoolean("auth")) {
      out.write(result.getString("msg"));
      return;
    }
    // 设置编码格式，前端后台统一是utf-8
    request.setCharacterEncoding("utf-8");
    response.setContentType("text/json;charset=utf-8");
    // Servlet上下文对象
    ServletContext servletContext = this.getServletConfig().getServletContext();
    // tomcat的项目路径
    String realPath = servletContext.getRealPath(UPLOAD_DIR) + "/";
    UserData.setUploadPath(realPath);
    String tempPath = servletContext.getRealPath(TEMP_UPLOAD_DIR) + "/";
    File tempPathFile = new File(tempPath);
    File realPathFile = new File(realPath);
    if (!tempPathFile.exists()) {
      tempPathFile.mkdirs();
    }
    if (!realPathFile.exists()) {
      realPathFile.mkdirs();
    }
    // 文件对象的工厂类
    DiskFileItemFactory factory = new DiskFileItemFactory();
    // 设置最大上传大小
    factory.setSizeThreshold(SINGLE_FILE_MAXSIZE);
    // 将临时文件夹交给文件对象的工厂类
    factory.setRepository(tempPathFile);
    // 创建一个上传文件的处理者
    ServletFileUpload upload = new ServletFileUpload(factory);
    // 设置所有请求的总大小
    upload.setSizeMax(TOTAL_FILE_MAXSIZE);
    // 解析request
    List<FileItem> items;
    try {
      items = upload.parseRequest(request);
      // 处理解析处理的请求对象
      Iterator<FileItem> iter = items.iterator();
      while (iter.hasNext()) {
        FileItem item = iter.next();
        //判断是文件还是文本
        if (!item.isFormField()) {
          // 过滤掉不支持的文件类型
          String fileRealName = item.getName();
          String ext = OtherTool.getFileExtName(fileRealName);
          if (!OtherTool.contain(extlist, ext)) {
            out.write("不支持的文件类型!");
            return;
          }
          // 文件名加上UUid，可以防止重复
          String fileName = UUID.randomUUID().toString().replace("-", "") + "." + ext;
//          String fileName = item.getName();
          System.out.println("UploadTestServlet file path:" + UPLOAD_DIR + fileName);
          //判断空
          if (fileName != null && !"".equals(fileName)) {
            // 将文件写到硬盘
            item.write(new File(realPath + fileName));

            // 将请求在服务器端转发给api的总控类去处理
            RequestDispatcher dispatcher = servletContext.getRequestDispatcher("/api/user/setMyInfo");
            MyHttpRequest myRequest = (MyHttpRequest) request;
            myRequest.setStatus(200);
            myRequest.setMethodName("setMyInfo");
            myRequest.setServletName("api");
            myRequest.setActionName("user");
            myRequest.setAttribute("filedName", fieldName);
            myRequest.setAttribute("value", fileName);
            // 设置接口应用的场景
            myRequest.setAttribute("scene", "upload");
            dispatcher.forward(request, response);
            //将图片地址保存到request中，再转发回给jsp
            //UPLOAD_DIR+fileName这个是相对路径，给前端页面
            //realPath+fileName是绝对路径
            //request.setAttribute("path", UPLOAD_DIR+fileName);
            //request.getRequestDispatcher("/test/UploadTest.jsp").forward(request, response);
//            out.write("ok");
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}